<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2018/6/14
 * Time: 0:32
 */

namespace App\Http\Eloquent;

use App\Game;
use App\GameExt;
use Illuminate\Support\Facades\DB;
use Ramsey\Uuid\Uuid;

class GameRepository implements Repository
{

    /**
     * find one
     *
     * @param array $where
     * @param array $select
     * @param array $orderBy
     * @return mixed
     */
    public static function find($id = 0, $select = ['*'])
    {
        return Game::query()->find($id);
        // TODO: Implement find() method.
    }

    /**
     * find first data
     * @return mixed
     */
    public static function first($where = [], $select = ['*'], $orderBy = ['id', 'desc'])
    {
        return Game::query()->where($where)->select($select)->orderBy($orderBy[0],$orderBy[1])->first();
        // TODO: Implement first() method.
    }

    /**
     *
     */
    public static function getRowsByMap($condition = []){
        $query = Game::query();
        $games = $query->get();
        foreach ($games as &$game) {
            $game->type = self::getGameType($game->type);
        }
        return $games->toArray();
    }

    public static function insertData($data){

        /*
        *
        *
 `name              varchar(255) NOT NULL DEFAULT '' COMMENT '游戏中文名称',
 `name_en           varchar(255) NOT NULL COMMENT '游戏英文名称',
 `app_id`           varchar(16) NOT NULL COMMENT '产品唯一字符串ID',
 `app_secret       ` varchar(255) NOT NULL COMMENT '产品唯一认证密钥',
 `package_name     ` varchar(255) NOT NULL COMMENT '安装包名',
 `version          ` varchar(255) NOT NULL COMMENT '最新版本名（提供给外部显示）',
 `version_no       ` int(11) NOT NULL DEFAULT '0' COMMENT '最新版本号（程序中使用）',
 `icon             ` varchar(255) DEFAULT NULL COMMENT '图标',
 `title            ` varchar(255) NOT NULL COMMENT '标题',
 `synopsis         ` varchar(512) DEFAULT NULL COMMENT '简介',
 `company_id       ` int(11) NOT NULL DEFAULT '0' COMMENT '提供商id',
 `recommend_code   ` tinyint(1) unsigned NOT NULL COMMENT '推荐状态码1高2低',
 `class            ` tinyint(1) unsigned NOT NULL DEFAULT '1' COMMENT '游戏类别 0=>''网页免费'',1=>''网页下载'',2=>''APK免费'',3=>''APK下载''',
 `type             ` tinyint(2) unsigned NOT NULL COMMENT '1-角色扮演,2:-桌面游戏,3-射击游戏,4-运动游戏,5-动作游戏,6-卡片游戏,7-冒险游戏,8-竞速游戏,9-益智游戏,10-策略游戏,11-音乐游戏，12-网页游戏',
 `total_size       ` int(11) unsigned NOT NULL COMMENT 'apk大小',
 `download_count   ` int(10) unsigned NOT NULL COMMENT '下载次数',
 `rateing_bar      ` float(4,2) unsigned NOT NULL COMMENT '星数',
 `create_time      ` int(11) unsigned NOT NULL COMMENT '创建时间',
 `update_time      ` int(11) NOT NULL COMMENT '更新时间',
 `reason           ` varchar(255) DEFAULT NULL COMMENT '审核理由',
 `status           ` tinyint(1) unsigned NOT NULL DEFAULT '3' COMMENT '游戏状态 1 正常   0下线  2预售 3 审核中  4审核失败',
        *
        * */

        $gameInsertD = [
            'name'          =>$data['name'],
            'name_en'       =>$data['name_en'],
            'app_id'        => str_random(16),
            'app_secret'    => Uuid::uuid4()->toString(),
            'package_name'  =>$data['package_name'] ?? '',
            'version'       =>$data['version'] ?? '',
            'version_no'    =>$data['version_no'] ?? 0,
            'icon'          =>$data['icon'] ?? '',
            'title'         =>$data['title'] ?? '',
            'synopsis'      =>$data['synopsis'] ?? '',
            'company_id'    =>$data['company_id'] ?? 1,
            'recommend_code'=>$data['recommend_code'] ?? 0,
            'class'         =>$data['class'],
            'type'          =>$data['type'],
            'total_size'    =>$data['total_size']?? 0,
            'download_count'=>$data['download_count']?? 0,
            'rateing_bar'   =>$data['rateing_bar']?? 0,
            'create_time'   =>time(),
            'update_time'   =>time(),
            'reason'        =>"",
            'status'        =>$data['status']??3
        ];


        DB::beginTransaction();
        $gameId =  Game::query()->insertGetId($gameInsertD);

        /**
         * 处理 支付相关资料
         */
        if($data['class'] == 1 || $data['class'] == 3)
        {
            $gameExt = [
                'game_id'=> $gameId,
                'shop_id'=>$data['shop_id'] ?? '',
                'public_key'=>$data['public_key'],
                'notify_url'=>$data['notify_url'],
                'whatsnew'=>$data['whatsnew']  ??  '',
                'apk_path'=>$data['apk_path']  ??  ''
            ];
        }else{
            $gameExt = [
                'game_id'=> $gameId,
                'shop_id'=>'',
                'public_key'=>'',
                'notify_url'=>'',
                'whatsnew'=>$data['whatsnew']  ??  '',
                'apk_path'=>$data['apk_path']  ??  ''
            ];

        }
        $result = GameExt::query()->insert($gameExt);

        if($gameId  && $result){
            DB::commit();
            return true;
        }
        DB::rollBack();
        return false;
    }

    public static function updateData($data){
        $gameD = [
            'name'          =>$data['name'],
            'name_en'       =>$data['name_en'],
            'package_name'  =>$data['package_name'] ?? '',
            'version'       =>$data['version'] ?? '',
            'version_no'    =>$data['version_no'] ?? 0,
            'icon'          =>$data['icon'] ?? '',
            'title'         =>$data['title'] ?? '',
            'synopsis'      =>$data['synopsis'] ?? '',
            'company_id'    =>$data['company_id'] ?? 1,
            'class'         =>$data['class'],
            'type'          =>$data['type'],
            'total_size'    =>$data['total_size']?? 0,
            'update_time'   =>time(),
        ];

        DB::beginTransaction();
        $gameR =  Game::query()->where('id',$data['id'])->update($gameD);

        /**
         * 处理 支付相关资料
         */
        if($data['class'] == 1 || $data['class'] == 3)
        {
            $gameExtD = [
                'public_key'=>$data['public_key'],
                'notify_url'=>$data['notify_url'],
                'apk_path'=>$data['apk_path']??''
            ];
        }else{
            $gameExtD = [
                'public_key'=>'',
                'notify_url'=>'',
                'apk_path'=>$data['apk_path']??''
            ];

        }
        $gameExtR = GameExt::query()->where('game_id',$data['id'])->update($gameExtD);

        var_dump($gameExtR);
        if($gameR  && ($gameExtR == 0 || $gameExtR) ){
            DB::commit();
            return true;
        }
        DB::rollBack();
        return false;
    }

    public static function getGameType($type)
    {
        switch (intval($type)){

            case 1:
                return '角色扮演';
                break;
            case 2:
                return '桌面游戏';
                break;
            case 3:
                return '射击游戏';
                break;
            case 4:
                return '运动游戏';
                break;
            case 5:
                return '动作游戏';
                break;
            case 6:
                return '卡片游戏';
                break;
            case 7:
                return '冒险游戏';
                break;
            case 8:
                return '竞速游戏';
                break;
            case 9:
                return '益智游戏';
                break;
            case 10:
                return '策略游戏';
                break;
            case 11:
                return '音乐游戏';
                break;
            default:
                return '网页游戏';
                break;
        }

    }
}